﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AmuTools;
using app.Api;
using System.Data;

namespace app.Models
{
    [Model(TableName = "t_demand_item", PrimaryKey = "id", IdentityInsert = false)]
    public class DemandItem
    {
        [Field(Description = "唯一标识符")]
        public int id { get; set; }


        [Field(DataType = "nvarchar(MAX)", Description = "编号 = dm + 时间戳 + 服务机构id")]
        public string number { get; set; }


        [Field(Description = "需求标题")]
        public string title { get; set; }


        [Field(Webable = false, Description = "发布需求者的ID")]
        public string member_id { get; set; }


        private Member _member = null;
        [Field(Storageable = false, Description = "发布需求者")]
        public Member member {
            get {
                if (_member == null) {
                    _member = G.DB.GetById<Member>(member_id);
                }
                return _member;
            }
            set {
                _member = value;
                member_id = _member.id.ToString();
            }
        }


        [Field(Description = "需求平台模块类别")]
        public string m_type { get; set; }


        [Field(Description = "需求类别，便于后期搜索分类")]
        public string demand_type { get; set; }


        [Field(DataType = "nvarchar(MAX)", Description = "需求内容")]
        public string demand_content {get;set;}


        [Field(Description = "需求地区，如可能是企业地区或者活动地区")]
        public string demand_area { get; set; }


        [Field(Description = "需求所属行业，逗号分隔的数组")]
        public string demand_industry { get; set; }


        [Field(DataType = "char(13)", Description = "开始日期")]
        public string demand_beginDate { get; set; }


        [Field(DataType = "char(13)", Description = "结束日期")]
        public string demand_endDate { get; set; }


        [Field(Description = "这是个啥玩意")]
        public string item_type { get; set; }


        [Field(DataType = "int", Description = "总投资额")]
        public int demand_invest { get; set; }


        [Field(DataType = "int", Description = "已投资额")]
        public int demand_presentInvest { get; set; }


        [Field(DataType = "int", Description = "需要投资额")]
        public int demand_needInvest { get; set; }


        [Field(Description = "查看次数")]
        public int view_times { get; set; }


        [Field(DataType = "nvarchar(MAX)", Description = "资金用途")]
        public string demand_use { get; set; }


        [Field(Description = "这是个啥玩意")]
        public string demand_operation { get; set; }


        [Field(Description = "抵押物")]
        public string demand_diya { get; set; }


        [Field(Description = "这是个啥玩意")]
        public string demand_master { get; set; }


        [Field(DataType = "nvarchar(11)", Description = "联系人手机号")]
        public string demand_tele { get; set; }


        [Field(Description = "联系人地址")]
        public string demand_address { get; set; }


        private string _appendix_ids = "";
        [Field(Description = "附件ID数组")]
        public string appendix_ids
        {
            get { return _appendix_ids; }
            set
            {
                _appendix_ids = value;
                _appendix = null;
            }
        }


        private List<UploadedFile> _appendix = null;
        [Field(Storageable = false, Description = "附件数组")]
        public List<UploadedFile> appendix
        {
            get
            {
                if (_appendix == null)
                {
                    if (appendix_ids == "") _appendix = new List<UploadedFile>();
                    else _appendix = G.DB.Get<UploadedFile>(string.Format("{0} in ({1})", G.DB.GetPrimaryKey<UploadedFile>(), appendix_ids)).GetFirstTableList<UploadedFile>();
                }
                return _appendix;
            }
        }


        [Field(DataType = "nvarchar(MAX)", Description = "备注")]
        public string demand_remark { get; set; }


        [Field(DataType = "char(13)", Description = "创建时间")]
        public string create_time { get; set; }


        [Field(Description = "需求状态")]
        public int status { get; set; }


        [Field(Webable = false, Description = "是否已被删除")]
        public int is_delete { get; set; }


        [Field(Description = "安全等级：0.无；1.服务机构可见；2.指定服务机构可见。")]
        public int safety_grade { get; set; }


        [Field(Description = "允许excel显示的字段")]
        static public Dictionary<string, string> ExcelColumns
        {
            get
            {
                Dictionary<string, string> dic = new Dictionary<string, string>();
                dic.Add("number", "编号");
                dic.Add("title", "需求名称");
                dic.Add("create_time", "添加时间");
                dic.Add("status", "状态");
                return dic;
            }
        }


        [Field(Description = "对excel字段的操作，如将时间戳转为时间字符串")]
        static public Dictionary<string, Func<object, object>> ExcelColumnsHandle
        {
            get
            {
                Dictionary<string, Func<object, object>> dic = new Dictionary<string, Func<object, object>>();
                dic.Add("create_time", value => {
                    return DateTimeEx.TimeStampToDatetime(value.ToString());
                });
                return dic;
            }
        }
    }
}